﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.ZB
{
    public partial class SubjectFileList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            
            AP_Attachment att = new AP_Attachment();
            att.Where.Link_ID.Value = SubID;
            att.Query.Load();
            {
                dg.DataSource = att.DefaultView;
                dg.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string fileName = Path.GetFileName(fuReport.PostedFile.FileName);
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            if (fileName != "")
            {
                //string sFileName = Path.GetExtension(fileName);

                //AttachmentController.GetInstance().DeleteFile(11);
                AttachmentController.GetInstance().UploadFileToDB(fuReport.PostedFile, (int)AttachmentType.File, SubID, fileName );

                LoadData();
            }

        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("SubID");
            HelperURL.Redirect("../Appraisal/SubjectDisplay.aspx?ID=" + SubID.ToString());
        }

        protected void dg_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int fileID = HelperDigit.ConvertToInt32(dg.Rows[e.RowIndex].Cells[0].Text);
            AttachmentController.GetInstance().DeleteFileDB(fileID);
            //AttachmentController.GetInstance().DownLoadDbFile(fileID);
            dg.Rows[e.RowIndex].Visible = false;
        }

        protected void dg_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int fileID = HelperDigit.ConvertToInt32(dg.Rows[e.NewSelectedIndex].Cells[0].Text);
            AttachmentController.GetInstance().DownLoadDbFile(fileID);
        }
    }
}
